package com.ymatou.query.seller.trading.api.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.query.seller.trading.api.parameter.GetSingleOrderInfoBean;
import com.ymatou.query.seller.trading.api.parameter.GetSingleOrderInfoResponse;
import com.ymatou.query.seller.trading.api.parameter.OrderEntities;
import com.ymatou.query.seller.trading.api.parameter.Sp_GetSingleOrderInfo_1;
import com.ymatou.query.seller.trading.api.parameter.Ymt_O_OrderNote;
import com.ymatou.query.seller.trading.api.parameter.Ymt_Order;
import com.ymatou.query.seller.trading.api.parameter.Ymt_OrderInfo;
import com.ymatou.query.seller.trading.api.parameter.Ymt_OrderState;
import com.ymatou.query.seller.trading.api.parameter.Ymt_OrderSummary;
import com.ymatou.query.seller.trading.api.service.GetSingleOrderInfoCall;
import com.ymatou.query.seller.trading.api.service.OrderCreate;
import com.ymatou.query.trading.api.parameter.OrderConverter;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.common.entitie.C2COrderInfo.C2COrderInfoExt;
import com.ymttest.database.model.Ymt_Bill;
import com.ymttest.database.model.Ymt_CreditDetail;
import com.ymttest.database.model.Ymt_OrderExt;
import com.ymttest.database.model.Ymt_OrderPostPay;
import com.ymttest.database.model.Ymt_OrderToBill;
import com.ymttest.database.model.Ymt_Order_Frozen;
import com.ymttest.database.model.Ymt_TradingItem;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 获取单个订单信息 FreeMaker Template自动生成代码
 */
@TestSuite
@SuppressWarnings("rawtypes")
public class Ts_GetSingleOrderInfo {

	private static GetSingleOrderInfoBean gsoibean;
	private static GetSingleOrderInfoCall gsoicall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取单个订单信息");
	}

	@Before
	public void caseUp() {

		gsoibean = new GetSingleOrderInfoBean();
		gsoicall = new GetSingleOrderInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(gsoicall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		GetSingleOrderInfoResponse expectedResponse = new GetSingleOrderInfoResponse();
		ConstructExpectedResponse(expectedResponse);
		GetSingleOrderInfoResponse actualResponse = gsoicall
				.getSingleOrderInfoResponse();
		com.ymatou.query.seller.trading.api.parameter.OrderInfo expect = expectedResponse
				.getOrderInfo();
		com.ymatou.query.seller.trading.api.parameter.OrderInfo actual = actualResponse
				.getOrderInfo();
		DataUtil.verifyEquals(expect.getOrderId(), actual.getOrderId(),
				"验证OrderInfo.OrderId");
	}

	private void ConstructExpectedResponse(GetSingleOrderInfoResponse response) {
		Sp_GetSingleOrderInfo_1 sp = new Sp_GetSingleOrderInfo_1();
		sp.setOrderId(DataUtil.Integerr.GetValueOrDefault(gsoibean.getOrderId()));
		sp.setUserId(DataUtil.Integerr.GetValueOrDefault(gsoibean.getUserId()));
		sp.execute();
		OrderEntities result = ConvertResult(sp);
		List<com.ymatou.query.seller.trading.api.parameter.OrderInfo> orderInfos = OrderConverter
				.AsOrderInfos(result, false);
		if (DataUtil.isNullOrEmpty(orderInfos)) {
			response.setOrderInfo(new com.ymatou.query.seller.trading.api.parameter.OrderInfo());
		} else {
			response.setOrderInfo(orderInfos.get(0));
		}
	}

	private OrderEntities ConvertResult(Sp_GetSingleOrderInfo_1 sp) {
		OrderEntities result = new OrderEntities();
		OrderEntities entity = new OrderEntities();
		List<Ymt_Order> Orders = new ArrayList<Ymt_Order>();
		List<Ymt_CreditDetail> CreditDetails = new ArrayList<Ymt_CreditDetail>();
		List<Ymt_O_OrderNote> OrderNotes = new ArrayList<Ymt_O_OrderNote>();
		List<Ymt_OrderState> OrderStates = new ArrayList<Ymt_OrderState>();
		List<Ymt_OrderPostPay> PostPays = new ArrayList<Ymt_OrderPostPay>();
		List<Ymt_OrderInfo> OrderInfos = new ArrayList<Ymt_OrderInfo>();
		List<Ymt_OrderSummary> OrderSummaries = new ArrayList<Ymt_OrderSummary>();
		List<Ymt_Order_Frozen> OrderFrozens = new ArrayList<Ymt_Order_Frozen>();
		List<Ymt_OrderToBill> OrderToBills = new ArrayList<Ymt_OrderToBill>();
		List<Ymt_Bill> Bills = new ArrayList<Ymt_Bill>();
		List<Ymt_TradingItem> TradingItems = new ArrayList<Ymt_TradingItem>();
		List<Ymt_OrderExt> OrderExts = new ArrayList<Ymt_OrderExt>();
		if (DataUtil.isNullOrEmpty(sp.getOrders()) == false) {
			for (Map m : sp.getOrders()) {
				Ymt_Order o = new Ymt_Order();
				DataUtil.mapToBean(o, m);
				Orders.add(o);
			}
		}
		if (DataUtil.isNullOrEmpty(sp.getCreditDetails()) == false) {
			for (Map m : sp.getCreditDetails()) {
				Ymt_CreditDetail o = new Ymt_CreditDetail();
				DataUtil.mapToBean(o, m);
				CreditDetails.add(o);
			}
		}
		if (DataUtil.isNullOrEmpty(sp.getNotes()) == false) {
			for (Map m : sp.getNotes()) {
				Ymt_O_OrderNote o = new Ymt_O_OrderNote();
				DataUtil.mapToBean(o, m);
				OrderNotes.add(o);
			}
		}
		if (DataUtil.isNullOrEmpty(sp.getStates()) == false) {
			for (Map m : sp.getStates()) {
				Ymt_OrderState o = new Ymt_OrderState();
				DataUtil.mapToBean(o, m);
				OrderStates.add(o);
			}
		}
		if (DataUtil.isNullOrEmpty(sp.getPostPays()) == false) {
			for (Map m : sp.getPostPays()) {
				Ymt_OrderPostPay o = new Ymt_OrderPostPay();
				DataUtil.mapToBean(o, m);
				PostPays.add(o);
			}
		}
		if (DataUtil.isNullOrEmpty(sp.getInfos()) == false) {
			for (Map m : sp.getInfos()) {
				Ymt_OrderInfo o = new Ymt_OrderInfo();
				DataUtil.mapToBean(o, m);
				OrderInfos.add(o);
			}
			if (DataUtil.isNullOrEmpty(sp.getSummarys()) == false) {
				for (Map m : sp.getSummarys()) {
					Ymt_OrderSummary o = new Ymt_OrderSummary();
					DataUtil.mapToBean(o, m);
					OrderSummaries.add(o);
				}
			}
			if (DataUtil.isNullOrEmpty(sp.getOrderFrozens()) == false) {
				for (Map m : sp.getOrderFrozens()) {
					Ymt_Order_Frozen o = new Ymt_Order_Frozen();
					DataUtil.mapToBean(o, m);
					OrderFrozens.add(o);
				}
			}
			if (DataUtil.isNullOrEmpty(sp.getOrderToBills()) == false) {
				for (Map m : sp.getOrderToBills()) {
					Ymt_OrderToBill o = new Ymt_OrderToBill();
					DataUtil.mapToBean(o, m);
					OrderToBills.add(o);
				}
			}
			if (DataUtil.isNullOrEmpty(sp.getBills()) == false) {
				for (Map m : sp.getBills()) {
					Ymt_Bill o = new Ymt_Bill();
					DataUtil.mapToBean(o, m);
					Bills.add(o);
				}
			}
			if (sp.getTradingItem().size() > 0) {
				Ymt_TradingItem o = new Ymt_TradingItem();
				DataUtil.mapToBean(o, sp.getTradingItem());
				TradingItems.add(o);
			}
			if (DataUtil.isNullOrEmpty(sp.getOrderExts()) == false) {
				for (Map m : sp.getOrderExts()) {
					Ymt_OrderExt o = new Ymt_OrderExt();
					DataUtil.mapToBean(o, m);
					OrderExts.add(o);
				}
			}
		}
		entity.setCreditDetails(CreditDetails);
		entity.setOrderExts(OrderExts);
		entity.setOrderFrozens(OrderFrozens);
		entity.setOrderInfos(OrderInfos);
		entity.setOrderNotes(OrderNotes);
		entity.setOrders(Orders);
		entity.setOrderStates(OrderStates);
		entity.setOrderSummaries(OrderSummaries);
		entity.setOrderToBills(OrderToBills);
		entity.setPostPays(PostPays);
		entity.setTradingItems(TradingItems);
		entity.setBills(Bills);
		entity.setTotalCount(DataUtil.isNullOrEmpty(result.getOrders()) ? 0
				: result.getOrders().size());
		return entity;
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSingleOrderInfo_001() {
		Logger.start(true, "获取单个订单信息成功");
		try {
			Logger.comment("准备订单数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			C2COrderInfoExt order = o.create1_M2cOrder(userId, sellerId);
			Logger.comment("完成准备订单数据");
			gsoibean.setOrderId(order.getiOrderId());
			gsoibean.setUserId(sellerId);
			// 发送请求
			gsoicall.setData(gsoibean);
			gsoicall.callService();
			Logger.comment("gsoicall send:" + gsoicall.getEntityString());
			Logger.comment("gsoicall response:" + gsoicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, gsoicall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetSingleOrderInfo_002() {
		Logger.start(true, "用户id为空");
		try {
			gsoibean.setOrderId(105293318);
			// 发送请求
			gsoicall.setData(gsoibean);
			gsoicall.callService();
			Logger.comment("gsoicall send:" + gsoicall.getEntityString());
			Logger.comment("gsoicall response:" + gsoicall.getReturnData());
			Logger.verifyEquals(ECode.ERROR400, gsoicall.getCode(), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetSingleOrderInfo_003() {
		Logger.start(true, "订单编号id为空");
		try {
			gsoibean.setUserId(3793376);
			// 发送请求
			gsoicall.setData(gsoibean);
			gsoicall.callService();
			Logger.comment("gsoicall send:" + gsoicall.getEntityString());
			Logger.comment("gsoicall response:" + gsoicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, gsoicall.getCode(), "验证Code");
			DataUtil.verify(null, gsoicall.getData().get("OrderInfo"), "验证Data.OrderInfo=null");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetSingleOrderInfo_004() {
		Logger.start(true, "用户id不存在");
		try {
			// 创建订单
			/*
			 * Map map = new OrderCallService().createOrder(); JSONArray
			 * OrderIds = (JSONArray) map.get("OrderIds"); int OrderId =
			 * OrderIds.getInt(0); int UserId = (Integer) map.get("UserId");
			 */
			gsoibean.setOrderId(105293318);
			gsoibean.setUserId(1);
			// 发送请求
			gsoicall.setData(gsoibean);
			gsoicall.callService();
			Logger.comment("gsoicall send:" + gsoicall.getEntityString());
			Logger.comment("gsoicall response:" + gsoicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, gsoicall.getCode(), "验证Code");
			DataUtil.verify(null, gsoicall.getData().get("OrderInfo"), "验证Data.OrderInfo=null");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSingleOrderInfo_005() {
		Logger.start(true, "获取单个订单信息成功,BusynessTypeList的值大于1个");
		try {
			DefaultWapper defaultWapper = new DefaultWapper();
			int orderId = (int) defaultWapper
					.getFrozenDetail("OrderId",
							"IsFrozen=1 group by orderid having count(1)>1")
					.get(0).get("OrderId");
			List<Map> FrozenDetails = defaultWapper.getFrozenDetail(
					"BusynessType", "OrderId = " + orderId);
			List<Integer> BusynessTypes = new ArrayList<Integer>();
			for (int i = 0; i < FrozenDetails.size(); i++) {
				BusynessTypes.add((int) FrozenDetails.get(i)
						.get("BusynessType"));
			}
			int SellerId = (int) new YmtOrdersWapper()
					.selectOrders("o.iOrderId = " + orderId).get(0)
					.get("iBuyerId");
			gsoibean.setOrderId(orderId);
			gsoibean.setUserId(SellerId);
			// 发送请求
			gsoicall.setData(gsoibean);
			gsoicall.callService();
			Logger.comment("gsoicall send:" + gsoicall.getEntityString());
			Logger.comment("gsoicall response:" + gsoicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, gsoicall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSingleOrderInfo_006() {
		Logger.start(true, "获取单个订单信息成功,BusynessTypeList的值=1个");
		try {
			DefaultWapper defaultWapper = new DefaultWapper();
			int orderId = (int) defaultWapper
					.getFrozenDetail("OrderId",
							"IsFrozen=1 group by orderid having count(1)=1")
					.get(0).get("OrderId");
			List<Map> FrozenDetails = defaultWapper.getFrozenDetail(
					"BusynessType", "OrderId = " + orderId);
			List<Integer> BusynessTypes = new ArrayList<Integer>();
			for (int i = 0; i < FrozenDetails.size(); i++) {
				BusynessTypes.add((int) FrozenDetails.get(i)
						.get("BusynessType"));
			}
			int SellerId = (int) new YmtOrdersWapper()
					.selectOrders("o.iOrderId = " + orderId).get(0)
					.get("iBuyerId");
			gsoibean.setOrderId(orderId);
			gsoibean.setUserId(SellerId);
			// 发送请求
			gsoicall.setData(gsoibean);
			gsoicall.callService();
			Logger.comment("gsoicall send:" + gsoicall.getEntityString());
			Logger.comment("gsoicall response:" + gsoicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, gsoicall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSingleOrderInfo_007() {
		Logger.start(true, "获取单个订单信息成功,BusynessTypeList=null");
		try {
			Logger.comment("准备订单数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			C2COrderInfoExt order = o.create1_M2cOrder(userId, sellerId);
			Logger.comment("完成准备订单数据");
			gsoibean.setOrderId(order.getiOrderId());
			gsoibean.setUserId(sellerId);
			// 发送请求
			gsoicall.setData(gsoibean);
			gsoicall.callService();
			Logger.comment("gsoicall send:" + gsoicall.getEntityString());
			Logger.comment("gsoicall response:" + gsoicall.getReturnData());
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}